package com.kady.Dao.impl;

import com.kady.Dao.FoodDao;
import com.kady.entity.Food;
import com.kady.entity.PageInfo;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: kady
 * \* Date: 2021/3/25
 * \* Time: 14:31
 * \* 千峰JAVA2101班
 * \* Description: 菜品Dao
 * \
 */
public class FoodDaoImpl extends BaseDao implements FoodDao {
    /**
     * 查询菜品列表
     */
    @Override
    public List<Map<String,Object>> selectFoodList(String keyword, PageInfo pageInfo, String typeId) throws SQLException {
        String sql = "SELECT food_id foodId,\n" +
                "\tf.food_name foodName,\n" +
                "\tf.price price,\n" +
                "\tf.vip_price vipPrice,\n" +
                "\tf.food_type_id foodTypeId,\n" +
                "\tf.food_image foodImage,\n" +
                "\tf.food_desc foodDesc,\n" +
                "\tft.`food_type_id` typeId,\n" +
                "\tft.`food_type_name` typeName\n" +
                "\tFROM food f INNER JOIN food_type ft ON f.`food_type_id`= ft.`food_type_id` WHERE f.food_name LIKE ? ";

        if(pageInfo!=null){
            List<Map<String,Object>> res = null;
            if(typeId!=null){
                sql = sql.concat(" AND f.food_type_id = ? ");
                sql = sql.concat(" limit ?,? ");
                res = selectInnerList(sql,keyword,typeId,pageInfo.getPageIndex(),pageInfo.getPageSize());
            }else{
                sql = sql.concat(" limit ?,? ");
                res = selectInnerList(sql,keyword,pageInfo.getPageIndex(),pageInfo.getPageSize());
            }

            return res;
        }

        List<Map<String,Object>> res = selectInnerList(sql,keyword);
        return res;
    }

    /**
     * 添加菜品
     */
    @Override
    public void addFood(Object... objs) throws SQLException {
        String sql = "INSERT INTO food (food_name,price,vip_price,food_type_id,food_image,food_desc) VALUES (?,?,?,?,?,?);";
        update(sql,objs);
    }

    /**
     * 修改菜品
     */
    @Override
    public void updateFood(Object... objs) throws SQLException {
        String sql = "UPDATE food SET food_name = ?,price = ?,vip_price = ?,food_type_id = ?,food_image = ?,food_desc = ?  WHERE food_id = ?;";
        update(sql,objs);
    }

    /**
     * 查询单个菜品
     */
    @Override
    public Food selectFood(String food_id) throws SQLException {
        String sql = "SELECT food_id foodId,\n" +
                "\tfood_name foodName,\n" +
                "\tprice,\n" +
                "\tvip_price vipPrice,\n" +
                "\tfood_type_id foodTypeId,\n" +
                "\tfood_image foodImage,\n" +
                "\tfood_desc foodDesc,\n" +
                "\t`food_type_id` typeId FROM food WHERE food_id = ?;";
        Food res = selectOne(Food.class,sql,food_id);
        return res;
    }

    /**
     * 通过菜系ID查询获取菜品列表
     */
    @Override
    public List<Food> selectFoodByTypeId(long typeId) throws SQLException {
        String sql = "SELECT food_id foodId,\n" +
                "\tfood_name foodName,\n" +
                "\tprice,\n" +
                "\tvip_price vipPrice,\n" +
                "\tfood_type_id foodTypeId,\n" +
                "\tfood_image foodImage,\n" +
                "\tfood_desc foodDesc,\n" +
                "\t`food_type_id` typeId FROM food WHERE food_type_id = ?;";
        List<Food> res = selectList(Food.class,sql,typeId);
        return res;
    }

    /**
     * 删除菜品
     */
    @Override
    public void deleteFood(String foodId) throws SQLException {
        String sql = "DELETE FROM food WHERE food_id = ?";
        update(sql,foodId);
    }

    /**
     * 通过传入的菜系ID判断计算菜品数量
     */
    @Override
    public long calListCount(String keyword, String typeId) throws SQLException {
        String sql = "SELECT count(1) FROM food WHERE food_name LIKE ? ";
        long res = 0;
        if(typeId!=null){
            sql = sql.concat(" AND food_type_id = ? ");
            res = selectCount(sql,keyword,typeId);
        }else{
            res = selectCount(sql,keyword);
        }
        return res;
    }

    /**
     * 通过菜品名称查询对应的菜品
     */
    @Override
    public Food selectFoodByName(String name) throws SQLException {
        String sql = "SELECT food_id foodId,\n" +
                "\tfood_name foodName,\n" +
                "\tprice,\n" +
                "\tvip_price vipPrice,\n" +
                "\tfood_type_id foodTypeId,\n" +
                "\tfood_image foodImage,\n" +
                "\tfood_desc foodDesc,\n" +
                "\t`food_type_id` typeId FROM food WHERE food_name = ? ";
        Food res = selectOne(Food.class,sql,name);
        return res;
    }
}
